/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.basic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.close.basic.dto.ClosedPersonAuditDTO;
import com.hopes.bizz.close.basic.dto.ClosedPersonExcelDTO;
import com.hopes.bizz.close.basic.entity.ClosedPersonEntity;
import com.hopes.bizz.close.basic.mapper.ClosedPersonMapper;
import com.hopes.bizz.close.basic.service.ClosedPersonService;
import com.hopes.bizz.close.basic.vo.ClosedPersonExcelVO;
import com.hopes.bizz.close.common.constant.BasicDicConstant;
import com.hopes.boss.common.core.constant.CommonConstants;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
 * 园区人员信息
 *
 * @author pig
 * @date 2023-10-23 11:21:57
 */
@Service
public class ClosedPersonServiceImpl extends ServiceImpl<ClosedPersonMapper, ClosedPersonEntity> implements ClosedPersonService {


    @Override
    public boolean save(ClosedPersonEntity entity) {
        entity.setApplyTime(Calendar.getInstance().getTime());
        entity.setInParkFlag(CommonConstants.DEFAULT_FALSE_FLAG);
        entity.setAuditStatus(BasicDicConstant.CLOSE_PERSON_AUDIT_STATUS_NOT_AUDIT);
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    @Override
    public boolean auditById(ClosedPersonAuditDTO closedPersonAuditDTO) {
        return lambdaUpdate().set(ClosedPersonEntity::getAuditTime, Calendar.getInstance().getTime())
                .set(ClosedPersonEntity::getAuditStatus, closedPersonAuditDTO.getAuditStatus())
                .set(ClosedPersonEntity::getAuditResultDesc, closedPersonAuditDTO.getAuditResultDesc())
                .eq(ClosedPersonEntity::getId, closedPersonAuditDTO.getId()).update();
    }

    @Override
    public ValidateImportBO<ClosedPersonExcelDTO> validateImport(List<ClosedPersonExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedPersonExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedPersonExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedPersonEntity importDtoToEntity(ClosedPersonExcelDTO excelDto) {
        ClosedPersonEntity entity= new ClosedPersonEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public ClosedPersonExcelVO exportEntityToVo(ClosedPersonEntity entity) {
        ClosedPersonExcelVO excelVO = new ClosedPersonExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

}
